//# sourceURL=http://uMapApp/Module/Bridge/BridgeSinglePillarModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../Module",
    "../../UI/Panel/FloatPanel",
    "../../UI/Panel/PanelBlock/PanelBlock",
    "../../UI/Panel/PanelBlock/ListBlock",
    "../../UI/Panel/PanelBlock/ChartBlock",
    "../../UI/Panel/PanelBlock/PropertiesBlock",
    "../../UI/Panel/PanelBlock/TreeBlock",
    "../../UI/Tree/Tree",
    "../../UI/Panel/Item/ListItem"
    , "./BridgeSinglePillar"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    PanelBlock,
    ListBlock,
    ChartBlock,
    PropertiesBlock,
    TreeBlock,
    Tree,
    ListItem,
    BridgeSinglePillar
) {

    function BridgeSinglePillarModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "单柱桥梁";
        _this.icon = _this.app.icons.bridge;
        _this.panels = [];
        _this.items = [];


        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock", "title": "单柱桥梁"});

        _this.listPanel.listBlock.filter = {
            "param": "bridgeSinglePillar.title"
        };

        // _this.listPanel.listBlock.sorter = {
        //     "param": {
        //         "value": "bridgeSinglePillar.sortKey",
        //         "label": "桩号"
        //     }
        // };

        _this.listPanel
            .append(_this.listPanel.listBlock);

        _this.panels.push(_this.listPanel);

        _this.app.ui.layout.right.append(_this.listPanel);
    }

    var _class = BridgeSinglePillarModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"           : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"     : {
            value: function (list) {

                var _this = this;

                _this.bridges = {};

                $.each(list, function (i, item) {

                    item.app = _this.app;

                    if (_this.bridgeModule.bridges[item.bridge]) {
                        item.dataSource = _this.bridgeModule.bridges[item.bridge].dataSource;
                    }

                    var bridgeSinglePillar = new BridgeSinglePillar(item);

                    _this.items.push(bridgeSinglePillar);


                    bridgeSinglePillar.listItem = new ListItem({
                        "title"             : bridgeSinglePillar.title,
                        // "subtitle"          : bridgeSinglePillar.bridge,
                        "icon"              : _this.app.icons.bridge.base64,
                        "bridgeSinglePillar": bridgeSinglePillar
                    });

                    bridgeSinglePillar.listItem.on("click", function () {
                        _this.activeItem(bridgeSinglePillar);
                        bridgeSinglePillar.layer.active();
                    });

                    _this.listPanel.listBlock.add(bridgeSinglePillar.listItem);

                });


            }
        },
        "clear"          : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"           : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {

                    if (_this.app.main3DLayer) {
                        _this.sysMain3DLayerOpened = _this.app.main3DLayer.isOpened;
                        _this.app.main3DLayer.close();
                    }

                    _this.isOpened = true;
                    _this.listPanel.open(false);

                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"       : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.bridgeModule.loadInit(function () {
                        _this.load({}, function (data) {
                            _this.data = data;
                            _this.isLoaded = true;
                            callback && callback();
                        });
                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"      : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"          : {
            value: function () {
                var _this = this;

                if (_this.sysMain3DLayerOpened) {
                    _this.app.main3DLayer.open();
                }

                _this.closePanel();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "closePanel"     : {
            value: function () {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "activeItem"     : {
            value: function (item) {
                var _this = this;

                if (!item.isActive) {
                    _this.unActiveAllItem();
                    item.active();
                    item.listItem.addClass("active");
                }

            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});